import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Hello world!
 *
 */
public class App 
{
    private static void demo1() {
        String secretKey = "123";

        JwtBuilder jwtBuilder = Jwts.builder()
                .setId("666")
                .setSubject("小马")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + (60 * 1000)))
                .signWith(SignatureAlgorithm.HS256, secretKey.getBytes())
                .claim("role", "admin");

        System.out.println(jwtBuilder.compact());
    }

    private static void demo2() {
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NjYiLCJzdWIiOiLlsI_pqawiLCJpYXQiOjE1NjEwMDkzMDMsImV4cCI6MTU2MTAwOTM2";
        String secretKey = "123";

        Claims claims = Jwts.parser()
                .setSigningKey(secretKey.getBytes())
                .parseClaimsJws(token)
                .getBody();



    }

    public static void main( String[] args )
    {
        String token = JwtUtil.createJWT(6 * 1000, new User(10, "tom", "123456"));

        System.out.println(token);

        // Claims 这个 claims 你就可以认为它是一个 map，里面是一个个 key-value
        Claims claims = JwtUtil.parseJWT(token, new User(10, "tom", "123456"));
        System.out.println("用户id：" + claims.getId());
        System.out.println("用户名称：" + claims.getSubject());
        System.out.println("登录时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(claims.getIssuedAt()));
        System.out.println("过期时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(claims.getExpiration()));
        System.out.println("用户角色：" + claims.get("role"));

    }
}
